<template>
  <div>
    <div class="row">
      <div v-for="column in colmunList" :key="column._id" class="col-4 mb-4">
        <div class="card h-100 shadow-sm" style="width: 18rem">
          <div class="card-body text-center">
            <img
              :src="column.avatar && column.avatar.url"
              class="rounded-circle border border-light w-25 my-3"
              alt="..."
            />
            <h5 class="card-title">{{ column.title }}</h5>
            <p class="card-text">{{ column.description }}</p>
            <router-link
              :to="`/column/${column._id}`"
              class="btn btn-outline-primary"
              >进入专栏</router-link
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from 'vue'
import { ColumnProps } from '../store'

export default defineComponent({
  name: 'ColumnList',
  props: {
    list: {
      type: Array as PropType<ColumnProps[]>, // as类型断言
      required: true // 属于一个必填项
    }
  },
  setup (props) {
    // 默认图片
    const colmunList = computed(() => {
      return props.list.map((column) => {
        if (!column.avatar) {
          // column.avatar = require('@/assets/logo.png')
          column.avatar = {
            url: require('../assets/logo.png')
          }
        }
        return column
      })
    })
    return {
      colmunList
    }
  }
})
</script>
